package adapter;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import pojo.NguoiDungPOJO;
import pojo.NhomPOJO;


public class ContactList {
	public static String test="<Contact><Nhom ID=\"1\" TenNhom=\"Nh￿m 1\"><NguoiDung TenDangNhap=\"phu\"/><NguoiDung TenDangNhap=\"truc\"/></Nhom><Nhom ID=\"2\" TenNhom=\"Nh�m 2\"><NguoiDung TenDangNhap=\"phu\"/></Nhom></Contact>\"";
	private ArrayList<GroupEntity> data;

	public ArrayList<GroupEntity> getData() {
		return data;
	}

	public void setData(ArrayList<GroupEntity> data) {
		this.data = data;
	}
	public NguoiDungPOJO getMember(int groupIndex, int memberIndex){
		return this.data.get(groupIndex).memberList.get(memberIndex);
	}
	public NhomPOJO getGroup(int groupIndex){
		return this.data.get(groupIndex).group;
	}
	public int getSize(){
		return this.data.size();
	}
	public void init(String xml){
		this.data=new ArrayList<GroupEntity>();
		
		try{
		Document doc=XMLBuilder.loadXMLFromString(xml);
		NodeList groupList=doc.getElementsByTagName("Nhom");
		
		if(groupList!=null){
			for(int i=0;i<groupList.getLength();i++){
				
				Element group=(Element) groupList.item(i);
				NodeList memberList=group.getChildNodes();
				
				NhomPOJO nhom=new NhomPOJO();
				ArrayList<NguoiDungPOJO> members=new ArrayList<NguoiDungPOJO>();
				
				nhom.setId(Integer.parseInt(group.getAttribute("ID")));
				nhom.setTenNhom(group.getAttribute("TenNhom"));
				
				for(int j=0;j<memberList.getLength();j++){
					NguoiDungPOJO nd=new NguoiDungPOJO();
					Element memberElement=(Element) memberList.item(j);
					nd.setId(1);
					nd.setTenDangNhap(memberElement.getAttribute("TenDangNhap"));
					members.add(nd);
				}
				this.data.add(new GroupEntity(nhom, members));
			}
		}
		}catch(Exception e){
			
		}
	}
}
